package cn.ibizlab.odoo.core.odoo_sale.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv;
import cn.ibizlab.odoo.core.odoo_sale.domain.Sale_advance_payment_inv;
import cn.ibizlab.odoo.core.odoo_sale.filter.Sale_advance_payment_invSearchContext;
import cn.ibizlab.odoo.core.odoo_sale.service.ISale_advance_payment_invService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[销售预付款发票] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Sale_advance_payment_invServiceImpl implements ISale_advance_payment_invService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Isale_advance_payment_invClientService sale_advance_payment_invClientService;

    @Override
    public void createBatch(List<Sale_advance_payment_inv> sale_advance_payment_invs, int batchSize) {

                        //sale_advance_payment_invClientService.createBatch(sale_advance_payment_invs,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Isale_advance_payment_inv clientModel = sale_advance_payment_invClientService.createModel();
        clientModel.setId(id);
        sale_advance_payment_invClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Sale_advance_payment_inv> sale_advance_payment_invs, int batchSize) {

        //sale_advance_payment_invClientService.updateBatch(sale_advance_payment_invs,batchSize) ;

    }

    @Override
    public boolean update(Sale_advance_payment_inv sale_advance_payment_inv) {
        boolean bOk = false ;
        Isale_advance_payment_inv clientModel = this.convert2Model(sale_advance_payment_inv,null);
        sale_advance_payment_invClientService.update(clientModel) ;
        this.convert2Domain(clientModel,sale_advance_payment_inv) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //sale_advance_payment_invClientService.removeBatch(sale_advance_payment_invs) ;

    }

    @Override
    public Sale_advance_payment_inv get(Integer id) {
        Isale_advance_payment_inv clientModel = sale_advance_payment_invClientService.createModel();
        clientModel.setId(id);
        sale_advance_payment_invClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Sale_advance_payment_inv get(Sale_advance_payment_inv sale_advance_payment_inv) {
        Sale_advance_payment_inv retsale_advance_payment_inv = this.get(sale_advance_payment_inv.getId()) ;
    	BeanCopier copier=BeanCopier.create(retsale_advance_payment_inv.getClass(), sale_advance_payment_inv.getClass(), false);
        copier.copy(retsale_advance_payment_inv, sale_advance_payment_inv, null);
        return sale_advance_payment_inv ;
    }

    @Override
    public boolean create(Sale_advance_payment_inv sale_advance_payment_inv) {
        boolean bOk = false ;
        Isale_advance_payment_inv clientModel = this.convert2Model(sale_advance_payment_inv,null);
        sale_advance_payment_invClientService.create(clientModel) ;
        this.convert2Domain(clientModel,sale_advance_payment_inv);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Sale_advance_payment_inv> searchDefault(Sale_advance_payment_invSearchContext context) {
        List<Sale_advance_payment_inv> list = new ArrayList<Sale_advance_payment_inv>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv> clientModelList = sale_advance_payment_invClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_advance_payment_inv>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据科目查询
     */
    @Override
    public Page<Sale_advance_payment_inv> selectByOdoo_deposit_account(Integer id){
        Sale_advance_payment_invSearchContext context = new Sale_advance_payment_invSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_advance_payment_inv> list = new ArrayList<Sale_advance_payment_inv>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv> clientModelList = sale_advance_payment_invClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_advance_payment_inv>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品查询
     */
    @Override
    public Page<Sale_advance_payment_inv> selectByOdoo_product(Integer id){
        Sale_advance_payment_invSearchContext context = new Sale_advance_payment_invSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_advance_payment_inv> list = new ArrayList<Sale_advance_payment_inv>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv> clientModelList = sale_advance_payment_invClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_advance_payment_inv>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Sale_advance_payment_inv> selectByOdoo_create(Integer id){
        Sale_advance_payment_invSearchContext context = new Sale_advance_payment_invSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_advance_payment_inv> list = new ArrayList<Sale_advance_payment_inv>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv> clientModelList = sale_advance_payment_invClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_advance_payment_inv>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Sale_advance_payment_inv> selectByOdoo_write(Integer id){
        Sale_advance_payment_invSearchContext context = new Sale_advance_payment_invSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_advance_payment_inv> list = new ArrayList<Sale_advance_payment_inv>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv> clientModelList = sale_advance_payment_invClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_advance_payment_inv clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_advance_payment_inv>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Isale_advance_payment_inv convert2Model(Sale_advance_payment_inv domain , Isale_advance_payment_inv model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = sale_advance_payment_invClientService.createModel() ;
        }

         if(domain.getDeposit_taxes_idDirtyFlag())
            model.setDeposit_taxes_id(domain.getDeposit_taxes_id());
         if(domain.getAmountDirtyFlag())
            model.setAmount(domain.getAmount());
         if(domain.getCountDirtyFlag())
            model.setCount(domain.getCount());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getAdvance_payment_methodDirtyFlag())
            model.setAdvance_payment_method(domain.getAdvance_payment_method());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getDeposit_account_id_textDirtyFlag())
            model.setDeposit_account_id_text(domain.getDeposit_account_id_text());
         if(domain.getProduct_id_textDirtyFlag())
            model.setProduct_id_text(domain.getProduct_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getProduct_idDirtyFlag())
            model.setProduct_id(domain.getProduct_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getDeposit_account_idDirtyFlag())
            model.setDeposit_account_id(domain.getDeposit_account_id());
        return model ;
    }

    public Sale_advance_payment_inv convert2Domain( Isale_advance_payment_inv model ,Sale_advance_payment_inv domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Sale_advance_payment_inv();
        }

        if(model.getDeposit_taxes_idDirtyFlag())
            domain.setDeposit_taxes_id(model.getDeposit_taxes_id());
        if(model.getAmountDirtyFlag())
            domain.setAmount(model.getAmount());
        if(model.getCountDirtyFlag())
            domain.setCount(model.getCount());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getAdvance_payment_methodDirtyFlag())
            domain.setAdvance_payment_method(model.getAdvance_payment_method());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getDeposit_account_id_textDirtyFlag())
            domain.setDeposit_account_id_text(model.getDeposit_account_id_text());
        if(model.getProduct_id_textDirtyFlag())
            domain.setProduct_id_text(model.getProduct_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getProduct_idDirtyFlag())
            domain.setProduct_id(model.getProduct_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getDeposit_account_idDirtyFlag())
            domain.setDeposit_account_id(model.getDeposit_account_id());
        return domain ;
    }


}
